package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/wailsapp/wails/v2/pkg/runtime"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
	"vueProject/controller"
	"vueProject/dbTools"
	"vueProject/models/sys_db"
)

// App struct
type App struct {
	ctx context.Context
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// Startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) Startup(ctx context.Context) {
	a.ctx = ctx
}

func (a *App) SetWindowsTitle(title string) {
	runtime.WindowSetTitle(a.ctx, title)
}

func (a *App) GetCtx() context.Context {
	return a.ctx
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
	return fmt.Sprintf("Hello %s, It's show time!", name)
}

func (a *App) Tree() string {
	return "This is Tree Method"
}

func (a *App) InitDb() interface{} {
	return dbTools.InitDb()
}

// CreateConnect 新建服务数据库信息
func (a *App) CreateConnect(connect sys_db.DbConnectList) interface{} {
	return controller.CreateConnect(connect)
}

// DeleteConnect 删除单个服务数据库(根据：ID)
func (a *App) DeleteConnect(ID int64) interface{} {
	return controller.DeleteConnect(ID)
}

// GetConnectList 获取服务数据库列表
func (a *App) GetConnectList() interface{} {
	//ConnectList, _ := json.Marshal(controller.GetConnectList())
	ConnectList := controller.GetConnectList()
	return map[string]interface{}{
		"code": 200,
		"msg":  "",
		"data": ConnectList,
		//"data": string(ConnectList),
	}
}

// ConnectSDB 连接服务数据库
// func (a *App) ConnectSDB(db controller.DbConnect) interface{} {
func (a *App) ConnectSDB(db int64) interface{} {
	return dbTools.ConnectSdb(db)
}

// CloseSDB 关闭服务数据库连接
func (a *App) CloseSDB() interface{} {
	return dbTools.CloseSdb()
}

// 检查版本更新-----------------------------------------------------------------------------------------------------------------------

// VersionInfo 用于解析从Gitee获取的版本信息JSON
type VersionInfo struct {
	Version      string `json:"version"`
	ReleaseNotes string `json:"release_notes"`
}

var currentVersion = "1.0.0"

// CheckForUpdates 检查是否有新版本
func (a *App) CheckForUpdates() {
	url := "https://gitee.com/your_username/your_repo/raw/master/latest_version.json"

	resp, err := http.Get(url)
	if err != nil {
		fmt.Printf("版本检查失败: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("读取响应体失败: %v\n", err)
		return
	}

	var info VersionInfo
	err = json.Unmarshal(body, &info)
	if err != nil {
		fmt.Printf("解析JSON失败: %v\n", err)
		return
	}

	if compareVersions(currentVersion, info.Version) < 0 {
		fmt.Printf("新版本可用！当前版本: %s，最新版本: %s\n", currentVersion, info.Version)
		fmt.Printf("查看更新日志: %s\n", info.ReleaseNotes)
	} else {
		fmt.Println("当前已是最新版本。")
	}
}

// compareVersions 比较两个版本号
func compareVersions(v1, v2 string) int {
	s1 := strings.Split(v1, ".")
	s2 := strings.Split(v2, ".")

	for i := 0; i < len(s1) && i < len(s2); i++ {
		num1, _ := strconv.Atoi(s1[i])
		num2, _ := strconv.Atoi(s2[i])
		if num1 < num2 {
			return -1
		} else if num1 > num2 {
			return 1
		}
	}

	if len(s1) < len(s2) {
		return -1
	} else if len(s1) > len(s2) {
		return 1
	}
	return 0
}
